package DEMO;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.*;
import org.xml.sax.SAXException;

public class ReadXmlFile {
	public static void main (String [] args){
		
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = factory.newDocumentBuilder();
			File file = new File("D:\\ThongTin.xml");
			Document doc = docBuilder.parse(file);
			
			//Get a list of all element in the document
				NodeList list = doc.getElementsByTagName("*");
				
				for(int i=0;i<list.getLength();i++){
					Element element = (Element) list.item(i);
					String nodeName = element.getNodeName();
					if(nodeName.equals("ThongTinSV")){
						System.out.println("ThongTinSV");
					}else if (nodeName.equals("MSSV")){
						System.out.println("\tMSSV: ID = " + element.getAttribute("ID"));
						}else if (nodeName.equals("HoTen")){
							System.out.println("\t\tHoTen: \t " + element.getChildNodes().item(0).getNodeValue());
							}else if (nodeName.equals("NamSinh")){
								System.out.println("\t\tNamSinh: "+element.getChildNodes().item(0).getNodeValue());
								}else if (nodeName.equals("Lop")){
									System.out.println("\t\tLop: \t" + element.getChildNodes().item(0).getNodeValue());
									}
					
				}
						
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
	}
}
